<template>
  <div class="department-container">
    <el-card>
      <template #header>
        <div class="header">
          <el-input
            v-model="searchQuery"
            placeholder="请输入部门名称搜索"
            style="width: 200px"
            clearable
            @clear="handleSearch"
          />
          <el-button type="primary" @click="handleAdd">
            <el-icon><Plus /></el-icon>
            新增部门
          </el-button>
        </div>
      </template>

      <el-table
        :data="tableData"
        style="width: 100%"
        v-loading="loading"
        row-key="id"
        default-expand-all
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="name" label="部门名称" />
        <el-table-column prop="leader" label="负责人" />
        <el-table-column prop="phone" label="联系电话" />
        <el-table-column prop="email" label="邮箱" />
        <el-table-column prop="status" label="状态">
          <template #default="{ row }">
            <el-tag :type="row.status === 1 ? 'success' : 'danger'">
              {{ row.status === 1 ? '正常' : '停用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleAdd(row)">新增</el-button>
            <el-button link type="primary" @click="handleEdit(row)">编辑</el-button>
            <el-button link type="danger" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { Plus } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const searchQuery = ref('')
const loading = ref(false)

const tableData = ref([
  {
    id: 1,
    name: '总公司',
    leader: '张三',
    phone: '13800138000',
    email: 'zhangsan@example.com',
    status: 1,
    children: [
      {
        id: 2,
        name: '技术部',
        leader: '李四',
        phone: '13800138001',
        email: 'lisi@example.com',
        status: 1
      },
      {
        id: 3,
        name: '市场部',
        leader: '王五',
        phone: '13800138002',
        email: 'wangwu@example.com',
        status: 1
      }
    ]
  }
])

const handleSearch = () => {
  // 实现搜索逻辑
}

const handleAdd = (row) => {
  // 实现添加部门逻辑
}

const handleEdit = (row) => {
  // 实现编辑部门逻辑
}

const handleDelete = (row) => {
  ElMessageBox.confirm('确认删除该部门及其所有子部门?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 实现删除部门逻辑
    ElMessage.success('删除成功')
  }).catch(() => {})
}
</script>

<style scoped>
.department-container {
  padding: 20px;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
